Snažím se zavolat AsyncTask pomocí metody z ParseQuerys Class, abych tyto informace použil v ListView mého Fragmentu lvRestaurantFrag. Problém je v tom, že metoda vrací null, protože AsyncTask nedorazí na onPostExecute. Nevím, jak to mohu opravit. Zde nechávám svůj kód: JSON Odkaz na API [ { "idBranchRestaurant": 1, "idRestaurant": 1, "restaurace":{ "idRestaurant": 1, "name": "Don Julio", "description": "La mejor parrilla para cualquier turista", "sociální síť":[ { "idSocialNetworkRestaurant": 1, "idRestaurant": 1, "idTypeSocialNetwork": 1, "value": "www.donjulio.com.ar", "typ":{ "idTypeSocialNetwork": 1, "name": "Webové stránky", "icon": "website_ic.jpg" } } ] }, "idRangePriceBranch": 1, "RangePrice": { "idRangePriceBranch": 1, "idBranchRestaurant": 1, "minimum": 300, "maximum": 600 }, "name": "Paraguay", "fotografie":[ { "idBranchPhoto": 4, "idBranchRestaurant": 1, "branch": null, "idUser": 0, "user": null, "photo": "entradaDonJulio.jpg" } ], "kuchyně":[ { "idCousine": 1, "name": "Parrilla" } ], "Jídelní lístek":[ { "idTypeMenu": 1, "name": "Mediodia", "idBranchRestaurant": 1, "plates": null }, { "idTypeMenu": 2, "name": "Noche", "idBranchRestaurant": 1, "plates": null } ], "filtr":[ { "idTypeFilter": 2, "name": "Family", "icon": "family_ic.jpg" } ], "servis":[ { "idService": 2, "name": "Comida para celiacos" }, { "idService": 1, "name": "Wifi" } ], "jízdní řád":[ { "idTimetableBranch": 11, "idBranchRestaurant": 1, "idDay": 2, "openingHour": "19 hs", "closedHour": "24 h", "den":{ "idDay": 2, "name": "úterý" } }, { "idTimetableBranch": 10, "idBranchRestaurant": 1, "idDay": 2, "openingHour": "12 hodin", "closedHour": "16 hs", "den":{ "idDay": 2, "name": "úterý" } }, { "idTimetableBranch": 9, "idBranchRestaurant": 1, "idDay": 1, "openingHour": "19 hs", "closedHour": "24 h", "den":{ "idDay": 1, "name": "pondělí" } }, { "idTimetableBranch": 8, "idBranchRestaurant": 1, "idDay": 1, "openingHour": "12 hodin", "closedHour": "16 hs", "den":{ "idDay": 1, "name": "pondělí" } }, { "idTimetableBranch": 12, "idBranchRestaurant": 1, "idDay": 3, "openingHour": "12 hodin", "closedHour": "16 hs", "den":{ "idDay": 3, "name": "středa" } }, { "idTimetableBranch": 13, "idBranchRestaurant": 1, "idDay": 3, "openingHour": "19 hs", "closedHour": "24 h", "den":{ "idDay": 3, "name": "středa" } }, { "idTimetableBranch": 14, "idBranchRestaurant": 1, "idDay": 4, "openingHour": "12 hodin", "closedHour": "16 hs", "den":{ "idDay": 4, "name": "čtvrtek" } }, { "idTimetableBranch": 15, "idBranchRestaurant": 1, "idDay": 4, "openingHour": "19 hs", "closedHour": "24 h", "den":{ "idDay": 4, "name": "čtvrtek" } }, { "idTimetableBranch": 16, "idBranchRestaurant": 1, "idDay": 5, "openingHour": "12 hodin", "closedHour": "16 hs", "den":{ "idDay": 5, "name": "pátek" } }, { "idTimetableBranch": 17, "idBranchRestaurant": 1, "idDay": 5, "openingHour": "19 hs", "closedHour": "2 hs", "den":{ "idDay": 5, "name": "pátek" } } ], "latitude": "87654322", "longitude": "23456789", "calification": null, "averageFood": 4.0, "averageService": 4.0, "averageAmbience": 4.0, "averageCalification": 4.0, "povýšení":[ { "idPromotion": 3, "idTypePromotion": 2, "code": "DIADELPADRE", "name": "Dia Del Padre Regalo", "startDate": "2017-06-26T00: 00: 00", "expireDate": "2017-06-18T00: 00: 00", "description": "postre de regalo", "value": 100, "typ":{ "idTypePromotion": 2, "name": "Dárek" } } ] }, { "idBranchRestaurant": 2, "idRestaurant": 2, "restaurace":{ "idRestaurant": 2, "name": "La Hormiga", "description": "el lugar explota de gente y en especial de gente de ort", "sociální síť":[ { "idSocialNetworkRestaurant": 2, "idRestaurant": 2, "idTypeSocialNetwork": 2, "value": "www.facebook.com/LaHormiga", "typ":{ "idTypeSocialNetwork": 2, "name": "Facebook", "icon": "fb_ic.jpg" } } ] }, "idRangePriceBranch": 1, "RangePrice": { "idRangePriceBranch": 1, "idBranchRestaurant": 1, "minimum": 300, "maximum": 600 }, "name": "Ort", "fotografie":[ { "idBranchPhoto": 5, "idBranchRestaurant": 2, "branch": null, "idUser": 0, "user": null, "photo": "entradaLaHormiga.jpg" } ], "kuchyně":[ { "idCousine": 2, "name": "Pizza" } ], "Jídelní lístek":[ { "idTypeMenu": 3, "name": "Mediodia", "idBranchRestaurant": 2, "plate": null } ], "filtr":[ { "idTypeFilter": 1, "name": "Se slevou", "icon": "discount_ic.jpg" } ], "servis":[ { "idService": 3, "name": "Aire acondicionado" }, { "idService": 1, "name": "Wifi" } ], "jízdní řád":[ { "idTimetableBranch": 18, "idBranchRestaurant": 2, "idDay": 6, "openingHour": "11 hodin", "closedHour": "15 hodin", "den":{ "idDay": 6, "name": "sobota" } }, { "idTimetableBranch": 19, "idBranchRestaurant": 2, "idDay": 7, "openingHour": "11 hodin", "closedHour": "15 hodin", "den":{ "idDay": 7, "name": "neděle" } } ], "latitude": "234567897", "longitude": "123456789", "calification": null, „averageFood“: 5,0, "averageService": 5.0, "averageAmbience": 5.0, "averageCalification": 5.0, "povýšení":[ ] }, { "idBranchRestaurant": 3, "idRestaurant": 3, "restaurace":{ "idRestaurant": 3, "name": "paja rota", "description": "La mejor parrilla de ramosmejia ", "sociální síť":[ { "idSocialNetworkRestaurant": 3, "idRestaurant": 3, "idTypeSocialNetwork": 3, "value": "twitter.com/Pajarota", "typ":{ "idTypeSocialNetwork": 3, "name": "Twitter", "icon": "ic_twitter.png" } } ] }, "idRangePriceBranch": 3, "RangePrice": { "idRangePriceBranch": 3, "idBranchRestaurant": 3, "minimum": 50, "maximum": 250 }, "name": "Ramos MEjia", "fotografie":[ { "idBranchPhoto": 6, "idBranchRestaurant": 3, "branch": null, "idUser": 0, "user": null, "photo": "EntradaPajaRota.jpg" } ], "kuchyně":[ { "idCousine": 1, "name": "Parrilla" } ], "Jídelní lístek":[ { "idTypeMenu": 4, "name": "Mediodia", "idBranchRestaurant": 3, "plate": null }, { "idTypeMenu": 5, "name": "Noche", "idBranchRestaurant": 3, "plate": null } ], "filtr":[ { "idTypeFilter": 2, "name": "Family", "icon": "family_ic.jpg" } ], "servis":[ { "idService": 1, "name": "Wifi" }, { "idService": 4, "name": "Carta de braille" } ], "jízdní řád":[ { "idTimetableBranch": 20, "idBranchRestaurant": 3, "idDay": 5, "openingHour": "12 hodin", "closedHour": "16 hs", "den":{ "idDay": 5, "name": "pátek" } }, { "idTimetableBranch": 21, "idBranchRestaurant": 3, "idDay": 5, "openingHour": "19 hs", "closedHour": "2 hs", "den":{ "idDay": 5, "name": "pátek" } }, { "idTimetableBranch": 22, "idBranchRestaurant": 3, "idDay": 6, "openingHour": "12 hodin", "closedHour": "16 hs", "den":{ "idDay": 6, "name": "sobota" } }, { "idTimetableBranch": 23, "idBranchRestaurant": 3, "idDay": 6, "openingHour": "19 hs", "closedHour": "2 hs", "den":{ "idDay": 6, "name": "sobota" } }, { "idTimetableBranch": 24, "idBranchRestaurant": 3, "idDay": 7, "openingHour": "12 hodin", "closedHour": "16 hs", "den":{ "idDay": 7, "name": "neděle" } } ], "latitude": "12345678", "longitude": "87654321", "calification": null, „averageFood“: 5,0, "averageService": 5.0, "averageAmbience": 5.0, "averageCalification": 5.0, "povýšení":[ ] }, { "idBranchRestaurant": 4, "idRestaurant": 4, "restaurace":{ "idRestaurant": 4, "name": "la espiga", "description": "el mejor lugar para comer pastas", "sociální síť":[ { "idSocialNetworkRestaurant": 4, "idRestaurant": 4, "idTypeSocialNetwork": 4, "value": "instagram.com/laespiga", "typ":{ "idTypeSocialNetwork": 4, "name": "Instagram", "icon": "ic_Instagram.png" } } ] }, "idRangePriceBranch": 4, "RangePrice": { "idRangePriceBranch": 4, "idBranchRestaurant": 4, "minimum": 80, "maximum": 150 }, "name": "Almagro", "fotografie":[ { "idBranchPhoto": 7, "idBranchRestaurant": 4, "branch": null, "idUser": 0, "user": null, "photo": "EntradaLaEspiga.jpg" } ], "kuchyně":[ { "idCousine": 4, "name": "Těstoviny" } ], "Jídelní lístek":[ { "idTypeMenu": 6, "name": "Tarde", "idBranchRestaurant": 4, "plate": null } ], "filtr":[ { "idTypeFilter": 1, "name": "Se slevou", "icon": "discount_ic.jpg" } ], "servis":[ { "idService": 1, "name": "Wifi" }, { "idService": 3, "name": "Aire acondicionado" } ], "jízdní řád":[ { "idTimetableBranch": 26, "idBranchRestaurant": 4, "idDay": 1, "openingHour": "16 hs", "closedHour": "19 hs", "den":{ "idDay": 1, "name": "pondělí" } }, { "idTimetableBranch": 27, "idBranchRestaurant": 4, "idDay": 2, "openingHour": "16 hs", "closedHour": "19 hs", "den":{ "idDay": 2, "name": "úterý" } }, { "idTimetableBranch": 28, "idBranchRestaurant": 4, "idDay": 3, "openingHour": "16 hs", "closedHour": "19 hs", "den":{ "idDay": 3, "name": "středa" } }, { "idTimetableBranch": 29, "idBranchRestaurant": 4, "idDay": 4, "openingHour": "16 hs", "closedHour": "19 hs", "den":{ "idDay": 4, "name": "čtvrtek" } }, { "idTimetableBranch": 30, "idBranchRestaurant": 4, "idDay": 5, "openingHour": "16 hs", "closedHour": "19 hs", "den":{ "idDay": 5, "name": "pátek" } } ], "latitude": "87657467", "longitude": "45254567", "calification": null, „averageFood“: 5,0, "averageService": 5.0, "averageAmbience": 5.0, "averageCalification": 5.0, "povýšení":[ ] } ] Třída ParseQuery private String server = "http://appmorfando.azurewebsites.net/api/"; ParsingObjects parse = new ParsingObjects (); // Dotaz na větve seznamu privátní větve ArrayList= nový ArrayList (); public ArrayList getAllBranch (int limit, int offset) { new BranchGetAll (). execute ("branch /", limit + "", offset + ""); zpětné větve; } soukromá třída BranchGetAll rozšiřuje AsyncTask > { protected void onPostExecute (ArrayList datos) { super.onPostExecute (data); pobočky = data; } @ Přepis Protected ArrayList doInBackground (String ... parametry) { Řetězec url = parametry [0] + parametry [1] + "/" + parametry [2]; ArrayList branchList = nový ArrayList (); OkHttpClient klient = nový OkHttpClient (); Žádost o požadavek = nový Request.Builder () .url (server + url) .stavět(); Řetězec resultado; Snaž se { Odezva odpověď = client.newCall (request) .execute (); // Llamo al API Rest servicio1 en ejemplo.com resultado = response.body (). string (); } chytit (IOException e) { Log.d ("Chyba", e.getMessage ()); // Chyba de Network vrátit null; } Snaž se{ JSONArray jsonBranch = nový JSONArray (resultado); pro (int i = 0; i listBranches; public View onCreateView (LayoutInflater inflater, ViewGroup group, Bundle data) { Zobrazit toReturn; toReturn = inflater.inflate (R.layout.frag_restaurant_lv, group, false); main = (MainActivity) getActivity (); pq = new ParseQuery (); restaurantItems = (ListView) toReturn.findViewById (R.id.lvRestaurantItems); // Zde bude null, takže velikost adaptéru je 0 listBranches = pq.getAllBranch (10,0); adaptér = nový lvRestaurantAdapter (listBranches, main); restaurantItems.setAdapter (adaptér); restaurantItems.setOnItemClickListener (toto); návrat doReturn; } @ Přepis public void onItemClick (AdapterView > parent, View view, int position, long id) { int idBranch = adapter.getId (pozice); main.BranchSelected (idBranch); } }
2021-02-24 08:20:37
2 věci zde: Jak víte, že váš asynctask nedorazí na onpostexecute? Váš připojený kód nepřepíše onpostexecute. Zkusme tuto metodu implementovat a potvrdíme jednoduchým logcatem nebo system.out. Váš asynctask získává objekty Json ze serveru a balí je do seznamu, který má být vrácen. Existuje spousta způsobů, jak to selhat, například špatné připojení k serveru nebo nesprávná analýza JSON. Pečlivě bych otestoval vaše připojení a datovou strukturu, abyste se ujistili, že null je něco, co způsobuje asynctask. Jedním z nejjednodušších způsobů testování je komentář celého kódu souvisejícího s připojením a jsonem a následné vrácení ručně vyplněného seznamu, o kterém víte, že funguje. To pomůže izolovat oblast, která selhává. 1 | Tvoje odpověď StackExchange.ifUsing ("editor", function () { StackExchange.using ("externalEditor", function () { StackExchange.using ("snippets", function () { StackExchange.snippets.init (); }); }); }, „code-snippets“); StackExchange.ready (funkce () { var channelOptions = { tagy: "" .split (""), id: "1" }; initTagRenderer ("". split (""), "" .split (""), channelOptions); StackExchange.using ("externalEditor", function () { // Po výstřižcích je nutné spustit editor, pokud jsou úryvky povoleny if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using ("snippets", function () { createEditor (); }); } else { createEditor (); } }); funkce createEditor () { StackExchange.prepareEditor ({ useStacksEditor: false, heartbeatType: 'answer', autoActivateHeartbeat: false, convertImagesToLinks: true, noModals: true, showLowRepImageUploadWarning: true, reputationToPostImages: 10, bindNavPrevention: true, postfix: "", imageUploader: { brandingHtml: "Powered by \ u003ca href = \" https: //imgur.com/ \ "\ u003e \ u003csvg class = \" svg-icon \ "width = \" 50 \ "height = \" 18 \ "viewBox = \ "0 0 50 18 \" fill = \ "none \" xmlns = \ "http: //www.w3.org/2000/svg \" \ u003e \ u003cpath d = \ "M46.1709 9.17788C46.1709 8.26454 46,2665 7,94324 47,1084 7,58816C47.4091 7,46349 47,7169 7,36433 48,0099 7,26993C48,9099 6,97977 49,672 6,73443 49,672 5,93063C49,672 5,22043 48,9832 4,61182 48,1414 4,61182C47,4335 4,61182 46,7256 4,91628 4,91650 4,91650 4,416 164 4,45 43,1481 6,59048V11.9512C43.1481 13,2535 43,6264 13,8962 44,6595 13,8962C45,6924 13,8962 46,1709 13,2535 46,1709 11,9512V9.17788Z \ "/ \ u003e \ u003cpath d = \" M32.492 10,1419432,144,14,144,14,144,44 41,5985 12,6954 41,5985 10,1419V6,59049C41,5985 5,28821 41,1394 4,66232 40,1061 4,66232C39,0732 4,66232 38,5948 5,2881 38,5948 6,59049V9,60062C38,5948 10,8521 38,2696 11,5455 37,0451 11,5455 11355 521 35,4954 960062V6.59049C35.4954 5,28821 35,0173 4,66232 34,0034 4,66232C32,9703 4,6632 32,492 5,28821 32,492 6,59049V10.1419Z \ "/ \ u003e \ u003cpath fill-rule = \" evenodd \ "clip-rule = \" evenodd \ " = \ "M25.6622 17.6335C27.8049 17.6335 29.3739 16.9402 30.2537 15.6379C30.8468 14.7755 30.9615 13.5579 30.9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C28.9913 4.66231 28.49.549.549.549.549.54.4550 .1369 4,56087 21,0134 6,57349 21,0134 9,27932C21.0134 11,9852 23,003 13,913 25,3754 13,913C26,5612 13,913 27,4607 13,4902 28,1109 12,6616C28.1109 12,7229 28,1161 12,77,712,123,121,121,121,121,121,121 15.2321 24.1352 14,9821 23,5661 14,7787 C23,176 14,6393 22,8472 14,5218 22,5437 14,5218C21,7977 14,5218 21,2429 15,0123 21,2429 15,6878721,2429 16,7375 22,9072 17,6335 25,6622 17,6335ZM24,1317 7,287 7,247 26,2417 26,2817 27.2119 7,09766 28,0918 7,94324 28,0918 9,27932C28.0918 10,6321 27,2311 11,5116 26,1024 11,5116C24,9737 11,5116 24,1317 10,6491 24,1317 9,27323 \ 19,8079 13,2535 19,8079 11,9512V8,12928C19,8079 5,82936 18,4879 4,62866 16,4027 4,62866C15,1594 4,62866 14,279 4,98375 13,3609 5,88013C12,653 5,05154 11,6581 4,62866 10,3573 4,62866C9,3433 6,62866 8,77 7,32 6,47 4,32 6,47 4,32 6,47 4,32 6,47 4,32 6,47 5,32 6,47 4,32 6,47 4,32 5.00066 5,28821 500066 6,59049V11,9512C5,00066 13,2535 5,47873 13,8962 6,51203 13,8962C7,54479 13,8962 8,0232 13,2535 8,0232 11,9512V8,90741C8,0232 7,58817 8,44431 6,91179 9,53458 6,91179C10,5104 8,10118,810 11,88 C13.4375 13.8962 13.9157 13.2535 13.9157 11.9512V8.90741C13.9157 7.58817 14.3365 6.91179 15.4269 6.91179C16.4027 6.91179 16.8045 7.58817 16.8045 8.94108V11.9512Z \ "/ \ u003e \ u003cpath d = \ "M3.31675 6.59049C3.31675 5.28821 2.83866 4.66232 1.82471 4.66232C0.791758 4.66232 0.313354 5.28821 0.313354 6.59049V11.9512C0.313354 13.2535 0.791758 13.8962 1.82471 13.8962C2.85711,9512V6,59049Z \ "/ \ u003e \ u003cpath d = \" M1.87209 0,42929C0,843612 0,400291 0 1,1159 0 1,98861C0 2,87869 0,822846 3,57676 1,87209 3,57676C2.90056 3,57676 3,7244 2,87869 3,7231 1,194 1,0100 \ "fill = \" # 1BB76E \ "/ \ u003e \ u003c / svg \ u003e \ u003c / a \ u003e", contentPolicyHtml: "Uživatelské příspěvky jsou licencovány pod \ u003ca href = \" https: //stackoverflow.com/help/licensing \ "\ u003ecc by-sa \ u003c / a \ u003e \ u003ca href = \" https://stackoverflow.com / legal / content-policy \ "\ u003e (obsahové zásady) \ u003c / a \ u003e", allowUrls: true }, onDemand: pravda, discardSelector: ".discard-answer" , okamžitěShowMarkdownHelp: true, enableTables: true, enableSnippets: true }); } }); Děkujeme, že jste přispěli odpovědí na Stack Overflow! Nezapomeňte na otázku odpovědět. Uveďte podrobnosti a sdílejte svůj výzkum! Ale vyhnout se ... Žádáme o pomoc, vysvětlení nebo reagujeme na jiné odpovědi. Vytváření prohlášení na základě názoru; podpořte je referencemi nebo osobními zkušenostmi. Další informace najdete v našich tipech na psaní skvělých odpovědí. Koncept uložen Koncept zahozen Zaregistrujte se nebo se přihlaste StackExchange.ready (funkce () { StackExchange.helpers.onClickDraftSave ('# login-link'); }); Zaregistrujte se pomocí Google Zaregistrujte se pomocí Facebooku Zaregistrujte se pomocí e-mailu a hesla Předložit Zveřejněte jako host název E-mailem Povinné, ale nikdy zobrazené StackExchange.ready ( funkce () { StackExchange.openid.initPostLogin ('. New-post-login', 'https% 3a% 2f% 2fstackoverflow.com% 2fquestions% 2f45538107% 2fcall-asynctack-from-other-class-and-return-null-android-studio% 23new-answer ',' question_page '); } ); Zveřejněte jako host název E-mailem Povinné, ale nikdy zobrazené Zveřejněte svou odpověď Vyřadit Kliknutím na „Odeslat odpověď“ vyjadřujete souhlas s našimi podmínkami služby, zásadami ochrany osobních údajů a zásadami používání souborů cookie Toto není odpověď, kterou hledáte? Přečtěte si další otázky týkající se značek java android json android-asynctask nebo položte vlastní otázku.